/**
 * Created by 峥桂 on 2017/5/22.
 */

var templateUtils = function templateUtils() {
    var saveDo = function () {
        var data = utils.getFormData('createScriptForm');
        $("#yuLan_hid").val('false');
        console.log(data);
        utils.submitForm('createScriptForm', '/sys/update.do', data, function (result) {
            if (result.success) {
                alert("新增成功！");
                window.location.href = '/sys/sysTemplateList.show';
            } else {
                alert(result.msg);
            }
        });
    };
    var updateDo = function () {
        var data = utils.getFormData('createScriptForm');
        $("#yuLan_hid").val('false');
        $.post('/sys/update.do', data, function (result) {
            if (result.success) {
                alert("保存成功！");
                window.location.href = '/sys/sysTemplateList.show';
            } else {
                alert(result.msg);
            }
        });

        /* utils.submitForm('createScriptForm', '/sys/update.do', data, function (result) {
             console.log(result);
             if (result.isSuccess) {
                 alert("保存成功！");
                 window.location.href = '/sys/sysTemplateList.show';
             } else {
                 alert(result.msg);
             }
         });*/
    };
    var yuLanDo = function () {
        var data = utils.getFormData('createScriptForm');
        $("#yuLan_hid").val('true');
        console.log(data);
        $.post('/sys/yulan.do', data, function (result) {
            if (result.success) {
                console.log(result)
                $("#templateContentYuLan").val(result.data);
            } else {
                alert(result.msg);
            }
        });
    };
    var initTablesSelect = function () {
        $.post(window.base + "/sys/showTablesSelect", {}, function (result) {
            console.log(result)
            if (result.success) {
                var data = result.data;
                var options_s = "";
                $(data).each(function (i, value) {
                    options_s += buildSelect(value.tableName);
                });
            } else {
                options_s = buildSelect("暂无");
            }

            $("#selectTable").empty().append(options_s);

        });
    };
    var buildSelect = function (content) {
        return "<li><a>" + content + "</a></li>"
    }
    return {
        saveDo: saveDo,
        updateDo: updateDo,
        yuLanDo: yuLanDo,
        initTablesSelect: initTablesSelect,
        buildSelect: buildSelect
    };
}();

function showScriptDiv(id, obj) {
    $("#templateContent").val('')
    $("#temp_id").val(id);
    $("#templateContent").val($("#content_" + id).val());
    //var $thisTDK = $(obj).parents("tr").find("td");
    //$("#templatetype").empty().val($thisTDK[4].innerHTML);
    templateUtils.initTablesSelect();
}
